<?php
define('LEGAL_OPER', true);
require('../../includes/admin-init.php');

//列表页
if($method == 'list'){
	$db= new DB();
	$roleTable = new BaseDao($db->getDb(),'admin_role','role_id');
	$roles = $roleTable->getAll(null, null,array('sort_order asc'));
	$smarty->assign("roles",$roles);
	$smarty->display("auth/role_list.htm");
	$db->close();
	exit;
}

//列表页
if($method == 'addui'){
	$smarty->assign("actions", getMenu());
	$smarty->display("auth/role_add.htm");
	exit;
}
//列表页
if($method == 'add'){
	$db= new DB();
	$db->beginTransaction();
	$roleTable = new BaseDao($db->getDb(),'admin_role','role_id');
	$mapbase = new BaseDao($db->getDb(),'admin_role_action_map');
	try{
		$sets = array();
		$sets['role_name'] = $_POST['role_name'];
		$sets['sort_order'] = $_POST['sort_order'];
		$roleTable->save($sets);
		$roleid = $db->getDb()->lastInsertId();
		foreach($_POST['actionids'] as $aid){
			$map = array();
			$map['role_id']= $roleid;
			$map['action_id']= intval($aid);
			$mapbase->save($map);
		}
		$db->commit();
		$url = '/sys-admin/action/auth/role.php?met=addsuccess';
		to_redirect($url);
	}catch(Exception $e){
		$db->rollback();
		$url = '/sys-admin/action/auth/role.php?met=addfail';
		to_redirect($url);
	}
	$db->close();
	exit;
}

if($method == 'addsuccess'){
	$links[0]['text'] = "角色添加";
	$links[0]['href'] = '/sys-admin/action/auth/role.php?met=addui';
	$links[1]['text'] = '角色列表';
	$links[1]['href'] = '/sys-admin/action/auth/role.php?met=list';
	to_message($smarty, "角色添加成功",0,$links);
}
if($method == 'addfail'){
	$links[0]['text'] = "角色添加";
	$links[0]['href'] = '/sys-admin/action/auth/role.php?met=addui';
	$links[1]['text'] = '角色列表';
	$links[1]['href'] = '/sys-admin/action/auth/role.php?met=list';
	to_message($smarty, "角色添加失败",1,$links);
}

//修改角色页面
if($method == 'editui'){
	$roleId = to_int($_GET['rid']);
	if($roleId > 0){
		$db= new DB();
		$roleTable = new BaseDao($db->getDb(),'admin_role','role_id');
		$role = $roleTable->getById($roleId);
		$mapbase = new BaseDao($db->getDb(),'admin_role_action_map');
		$pmenu = getMenu();
		
		$maps = $mapbase->getAll("role_id=?", array($roleId));
		if(count($maps)>0){
			foreach($maps as $entry){
				$actionid = $entry['action_id'];
				foreach ($pmenu as $key0 => $parent){
					if($parent['action_id'] == $actionid){
						$pmenu[$key0]['ischecked'] = 1;
					}
					if($parent['childs'] != null && count($parent['childs']) > 0){
						foreach ($pmenu[$key0]['childs'] as $key1 => $child1){
							if($child1['action_id'] == $actionid){
								$pmenu[$key0]['childs'][$key1]['ischecked'] = 1;
							}
							if($child1['childs'] != null && count($child1['childs']) > 0){
								foreach ($pmenu[$key0]['childs'][$key1]['childs'] as $key2 => $child2){
									if($child2['action_id'] == $actionid){
										$pmenu[$key0]['childs'][$key1]['childs'][$key2]['ischecked'] = 1;
									}
								}
							}
						}
					}
				}
			}
		}
		$smarty->assign("actions",$pmenu);
		$smarty->assign("role",$role);
		$smarty->display("auth/role_edit.htm");
		$db->close();
	}else{
		$links=array();
		$links[0]['text'] = '角色列表';
		$links[0]['href'] = '/sys-admin/action/auth/role.php?met=list';
		to_message($smarty,"数据不正确",1,$links);
	}
	exit;
}

//修改角色
if($method == 'edit'){
	$roleId = to_int($_POST['role_id']);
	$actionids=  $_POST['actionids'];
	$db = new DB();
	$db->beginTransaction();
	$base = new BaseDao($db->getDb(),'admin_role','role_id');
	$mapbase = new BaseDao($db->getDb(),'admin_role_action_map');
	try{
		$sets = array();
		$sets['role_name'] = $_POST['role_name'];
		$sets['sort_order'] = $_POST['sort_order'];
		$base->update($sets, "role_id=?", array($roleId));
		$mapbase->delete('role_id=?', array($roleId));
		foreach($_POST['actionids'] as $aid){
			$map = array();
			$map['role_id']= $roleId;
			$map['action_id']= intval($aid);
			$mapbase->save($map);
		}
		$db->commit();
		$url = '/sys-admin/action/auth/role.php?met=editsuccess';
		to_redirect($url);
	}catch(Exception $ex){
		$db->rollback();
		$url = '/sys-admin/action/auth/role.php?met=editfail';
		to_redirect($url);
	}
	$db->close();
	exit;
}

if($method == 'editsuccess'){
	$links[0]['text'] = '角色列表';
	$links[0]['href'] = '/sys-admin/action/auth/role.php?met=list';
	to_message($smarty, "角色修改成功",0,$links);
}
if($method == 'editfail'){
	$links[0]['text'] = '角色列表';
	$links[0]['href'] = '/sys-admin/action/auth/role.php?met=list';
	to_message($smarty, "角色修改失败",1,$links);
}


//删除角色
if($method == 'delete'){
	$roleId = to_int($_GET['rid']);
	if($roleId > 0){
		$db = new DB();
		$db->beginTransaction();
		$base = new BaseDao($db->getDb(),'admin_role','role_id');
		$base->delete('role_id=?', array($roleId));
		$db->commit();
		$db->close();
		echo '1';
		exit;
	}
	echo '0';
	exit;
}



//获取权限选项
function getMenu(){
	$pmenu = array();
	$db = new DB();
    $base = new BaseDao($db->getDb(),'admin_action','action_id');
	$pmenu = array();
	$menu = $base->getAll('is_active=?', array(1),array('sort_order asc'));
	if($menu != null && count($menu)>0){
		foreach ($menu as $parent){
			if($parent['parent_id'] == null){
				$arr1 = array();
				foreach ($menu as  $key1=>$child1){
					if($child1['parent_id'] != null && $child1['parent_id'] == $parent['action_id']){
						$arr2 = array();
						foreach ($menu as  $key2=>$child2){
							if($child2['parent_id'] != null && $child2['parent_id'] == $child1['action_id']){
								$arr2[] = $child2;
							}
						}
						if(count($arr2) > 0){
							$child1['childs'] = $arr2;
						}
						$arr1[] = $child1;
					}
				}
				if(count($arr1) > 0){
					$parent['childs'] = $arr1;
				}
				$pmenu[] = $parent;
			}
		}
	}
	$db->close();
	return $pmenu;
}

